package com.ouheng.shop.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ouheng.common.annotation.Log;
import com.ouheng.common.utils.PageUtils;
import com.ouheng.common.utils.Query;
import com.ouheng.common.utils.R;
import com.ouheng.common.utils.StringUtils;
import com.ouheng.shop.domain.ProductPriceDO;
import com.ouheng.shop.service.ProductPriceService;

/**
 * 商品价格
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2018-08-24 11:14:38
 */
 
@Controller
@RequestMapping("/shop/productPrice")
public class ProductPriceController {
	@Autowired
	private ProductPriceService productPriceService;
	
	
	@GetMapping()
	@RequiresPermissions("shop:productPrice:productPrice")
	String ProductPrice(Long id,Model model){
		model.addAttribute("pid", id);
	    return "shop/productPrice/productPrice";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("shop:productPrice:productPrice")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<ProductPriceDO> productPriceList = productPriceService.list(query);
		int total = productPriceService.count(query);
		PageUtils pageUtils = new PageUtils(productPriceList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("shop:productPrice:add")
	String add(Long id,Model model){
		model.addAttribute("pid", id);
	    return "shop/productPrice/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("shop:productPrice:edit")
	String edit(@PathVariable("id") Long id,Model model){
		ProductPriceDO productPrice = productPriceService.get(id);
		model.addAttribute("productPrice", productPrice);
	    return "shop/productPrice/edit";
	}
	
	/**
	 * 保存
	 */
	@Log("添加商品价格")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("shop:productPrice:add")
	public R save( ProductPriceDO productPrice){
		if(productPrice.getPromoteprice()==null||("").equals(productPrice.getPromoteprice())){
			productPrice.setPromoteprice(productPrice.getPrice());
		}
		if(productPriceService.save(productPrice)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@Log("修改商品价格")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("shop:productPrice:edit")
	public R update( ProductPriceDO productPrice){
		if(productPrice.getPromoteprice()==null||("").equals(productPrice.getPromoteprice())){
			productPrice.setPromoteprice(productPrice.getPrice());
		}
		productPriceService.update(productPrice);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@Log("删除商品价格")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("shop:productPrice:remove")
	public R remove( Long id){
		if(productPriceService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@Log("批量删除商品价格")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("shop:productPrice:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		productPriceService.batchRemove(ids);
		return R.ok();
	}
	
}
